<?php

namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request; //命名空间的三元素：常量，方法和类
use DB;
use App\Http\Model\Gonggao;

class GonggaoController extends Controller
{
    // 显示公告信息
    public function ShowGonggao(){
        $result = Gonggao::paginate(5);
        return view('admin/gonggao/gonggao',compact('result'));
    }

    // 删除公告
    public function GonggaoDel($id){
        // Gonggao::where('id',$id)->delete();
        // 删除
        Gonggao::find($id)->delete();

        // 强制删除
        // Gonggao::find($id)->forceDelete();

        // if($result>0){
        //     return back();
        // }
        return ['status'=>0,'msg'=>'删除成功'];
    }
 
     // 新增公告
     public function GonggaoAdd(){
        $data['title']=$_POST['title'];
        $data['content']=$_POST['textarea'];
        $result = Gonggao::insert($data);
        if($result>0){
            return redirect('admin/gonggao');
        }
        else
        {
            return back();
        }
    }

      // 显示修改公告信息
      public function GonggaoExit($id){
        $result = Gonggao::where('id',$id)->first();
        return view('admin/gonggao/xggonggao',compact('result'));
    }

     // 修改公告
     public function GonggaoUp(){
        $id = $_POST['id'];
        $data['title']=$_POST['title'];
        $data['content']=$_POST['textarea'];
        $result = Gonggao::where('id',$id)->update($data);
        if($result>0){
            return redirect('admin/gonggao');
        }
        else
        {
            return back();
        }
    }
    
    // 复选框删除
    public function GonggaoDelall(Request $request){
      
        foreach ($_POST['checked'] as $key => $id) {
          $result = Gonggao::where('id',$id)->delete();
        }
        if($result>0){
            return redirect('admin/gonggao');
        }
        else
        {
            return back();
        }
       
    }
    
}